﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Workflow.Application.DTOs.Input;
using Workflow.Application.DTOs.Output;
using Workflow.Application.ErrorCode;
using Workflow.Application.IService;
using Workflow.Domain.Models;

namespace Workflow.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WorkflowController : ControllerBase
    {
        private readonly ILogger<WorkflowController> logger;
        private readonly IWorkflowService workflowService;

        public WorkflowController(IWorkflowService workflowService, ILogger<WorkflowController> logger)
        {
            this.workflowService = workflowService;
            this.logger = logger;   
        }


        /// <summary>
        /// 获取我的待办任务
        /// </summary>
        /// <param name="input">查询参数，包含分页、流程标题、模板组、申请人和创建时间范围</param>
        /// <returns>待办任务列表</returns>
        [HttpPost]
        public async Task<ApiPaging<MyTaskDto>> GetMyTasks(GetMyTasksInput input)
        {
            return await workflowService.GetMyTasks(input);
        }

        /// <summary>
        /// 获取流程业务实例列表
        /// </summary>
        /// <param name="input">查询参数，包含分页、流程标题、申请人和创建时间范围</param>
        /// <returns>流程业务实例列表</returns>
        [HttpPost]
        public async Task<ApiPaging<WorkflowInstanceDto>> GetWorkflowInstances(GetWorkflowInstanceInput input)
        {
            return await workflowService.GetWorkflowInstances(input);
        }

        /// <summary>
        /// 获取流程详情
        /// </summary>
        /// <param name="input">查询参数，包含流程实例ID</param>
        /// <returns>流程详情，包括基本信息、表单数据和审批记录</returns>
        [HttpGet("detail")]
        public async Task<ApiResult<WorkflowDetailDto>> GetWorkflowDetail([FromQuery] GetWorkflowDetailInput input)
        {
            return await workflowService.GetWorkflowDetail(input);
        }
        /// <summary>
        /// 流程模板添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddDefinition(WorkflowDefinitionAddInput input)
        {
            return await workflowService.AddDefinition(input);
        }
        /// <summary>
        /// 流程发起
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> StartWorkflow(WorkflowStartInput input)
        {
            return await workflowService.StartWorkflow(input);
        }
        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Audit(AuditInput input)
        {
            return await workflowService.Audit(input);
        }
        /// <summary>
        /// 获取流程模板列表
        /// </summary>
        /// <param name="input">查询参数，包含分页、标题和分组条件</param>
        /// <returns>流程模板列表</returns>
        [HttpPost]
        public async Task<List<WorkflowDefinitionDto>> GetDefinitions(GetWorkflowInput input)
        {
            return await workflowService.GetDefinitions(input);
        }
    }
}
